package com.yidu.qx.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.qx.domain.*;
import com.yidu.qx.service.CharacterService;
import com.yidu.qx.service.ModuleService;
import com.yidu.qx.service.RolesModulesImpl;
import com.yidu.qx.service.RolesModulesService;
import com.yidu.qx.utils.BaseMessage;
import com.yidu.qx.utils.Pages;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: jurisdiction
 * @description: 角色控制器
 * @author: 张超
 * @create: 2019-09-26 18:32
 */

@Controller
@RequestMapping("CharacterController")
public class CharacterController {

    @Resource
    CharacterService characterService;


    @Resource
    RolesModulesService rolesModulesService;


    @RequestMapping("addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(String json, QxCharacterEntity qxCharacterEntity){
        BaseMessage baseMessage = new BaseMessage();
        Gson gson = new Gson();
        List<JsonM> list =gson.fromJson(json, new TypeToken<List<JsonM>>() {}.getType());


        String ids = "";
        //循环上级qx
        for (Iterator<JsonM> iterator = list.iterator(); iterator.hasNext(); ) {

            JsonM jsonM = iterator.next();
            List<JsonM> jsonMS = jsonM.getChildren();
            //ids+=jsonM.getId()+"&";
            //循环下级
            for (Iterator<JsonM> jsonMIterator = jsonMS.iterator(); jsonMIterator.hasNext(); ) {
                JsonM next = jsonMIterator.next();
                ids+=next.getId()+"&";
            }
        }

        if(StringUtils.isEmpty(qxCharacterEntity.getChaId())){
            characterService.add(qxCharacterEntity);
        }else{
            characterService.update(qxCharacterEntity);
        }



        if(ids.length()>0){
            String[] idss = ids.split("&");

            //先把关于这个角色的模板权限清空
            QxRolesModulesEntity deleteAll = new QxRolesModulesEntity();
            deleteAll.setRolId(qxCharacterEntity.getChaId());
            rolesModulesService.deleteAll(deleteAll);

            //循环id数组
            for (int i = 0; i < idss.length; i++) {
                // new出关联对象
                QxRolesModulesEntity qxRolesModulesEntity = new QxRolesModulesEntity();

                //放入角色id
                qxRolesModulesEntity.setChaId(qxCharacterEntity.getChaId());

                //放入模板id
                qxRolesModulesEntity.setModId(idss[i]);
                rolesModulesService.add(qxRolesModulesEntity);
            }
        }

        baseMessage.setState(1);
        baseMessage.setMsg("编辑成功");
        return baseMessage;
    }


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public QxCharacterEntity findById(String id){
        return characterService.findById(id);
    }


    /**
     * 查询列表
     * @param qxCharacterEntity
     * @param pages
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object> findAll(QxCharacterEntity qxCharacterEntity, Pages pages,int page){
        pages.setCurPage(page);
        return characterService.findAll(qxCharacterEntity,pages);
    }

    @ResponseBody
    @RequestMapping("delete")
    public BaseMessage delete(QxCharacterEntity qxCharacterEntity){
        BaseMessage baseMessage = new BaseMessage();
        characterService.delete(qxCharacterEntity);
        baseMessage.setState(1);
        baseMessage.setMsg("删除成功");
        return baseMessage;
    }

    @ResponseBody
    @RequestMapping("deleteAll")
    public BaseMessage deleteAll(String ids){
        BaseMessage baseMessage = new BaseMessage();
        String id[] = ids.split(",");
        for (int i = 0;i<id.length;i++){
            QxCharacterEntity qxCharacterEntity = new QxCharacterEntity();
            qxCharacterEntity.setChaId(id[i]);
            characterService.delete(qxCharacterEntity);
        }

        baseMessage.setState(1);
        baseMessage.setMsg("删除成功");
        return baseMessage;
    }

    @RequestMapping("index")
    @ResponseBody
    public Map<String,Object> index(QxUserEntity qxUserEntity){
        return characterService.index(qxUserEntity);
    }

}